/**
 * Copyright (c) 2019, ShangHai YTO Co., Ltd.
 * All right reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF YTO
 * CO., LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED
 * TO THIRD PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 * WITHOUT THE PRIOR WRITTEN PERMISSION OF YTO
 * CO., LTD.
 */
package org.sc.xl.easyexcel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.List;

/**
 * @description: TODO 请在此添加描述
 * @reason: TODO ADD REASON(可选)
 * @author FXL
 *
 * @date 2024/10/20 16:41 
 * @since JDK 1.8
 */
public class CustomCellWidthHandler extends AbstractColumnWidthStyleStrategy {


    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        Sheet sheet = writeSheetHolder.getSheet();
        int columnIndex = cell.getColumnIndex();
        int lastColumnIndex = writeSheetHolder.getHead().size() - 1;
        if (lastColumnIndex == columnIndex) {
            sheet.setColumnWidth(columnIndex, (int) Math.ceil(35 * 256));
        }
        if (lastColumnIndex - 1 == columnIndex) {
            sheet.setColumnWidth(columnIndex, (int) Math.ceil(1 * 256));
        }
        if (columnIndex > 3 && columnIndex < lastColumnIndex - 1) {
            sheet.setColumnWidth(columnIndex, 2048);
        }
        if (3 == columnIndex) {
            sheet.setColumnWidth(columnIndex, (int) Math.ceil(15.5 * 256));
        }
        if (2 == columnIndex) {
            sheet.setColumnWidth(columnIndex, (int) Math.ceil(17 * 256));
        }
        if (1 == columnIndex) {
            sheet.setColumnWidth(columnIndex, (int) Math.ceil(9 * 256));
        }
        if (0 == columnIndex) {
            sheet.setColumnWidth(columnIndex, (int) Math.ceil(0.5 * 256));
        }
    }
}